package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.YddAdvposDto;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.service.IYddAdvimgService;
import com.itheima.ydd.service.IYddAdvposService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 */
@Slf4j
@RestController
@RequestMapping("/ydd_advpos")
public class YddAdvposController {

    @Autowired
    private IYddAdvposService yddAdvposService;

    @Autowired
    private IYddAdvimgService yddAdvimgService;

    /**
     * 广告位列表-分页
     *
     * @param advposdesc
     * @param advposname
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData list(String advposdesc, String advposname, int page, int limit){
        Page<YddAdvpos> advposPage = new Page<>(page, limit);
        Page<YddAdvposDto> advposDtoPage = new Page<>();
        LambdaQueryWrapper<YddAdvpos> lqw = new LambdaQueryWrapper<>();
        //条件
        lqw.like(StringUtils.isNotBlank(advposdesc), YddAdvpos::getAdvposdesc, advposdesc);
        lqw.like(StringUtils.isNotBlank(advposname), YddAdvpos::getAdvposname, advposname);
        yddAdvposService.page(advposPage,lqw);
        int count = yddAdvposService.count(lqw);

        BeanUtils.copyProperties(advposPage,advposDtoPage,"records");
        List<YddAdvpos> advposList = advposPage.getRecords();
        //根据这个id统计广告图里的Advimgpos有多少个相同****理解
        List<YddAdvposDto> advposDtoslist = advposList.stream().map((it) -> {
            YddAdvposDto advposDto = new YddAdvposDto();
            BeanUtils.copyProperties(it, advposDto);
            //缺少图片数量  统计  id出现次数
            //这个里面统计id次数然后赋值给advposnum就好了啊
            LambdaQueryWrapper<YddAdvimg> lqw1 = new LambdaQueryWrapper<>();
            lqw1.eq(YddAdvimg::getAdvimgpos, it.getId());
            int count1 = yddAdvimgService.count(lqw1);
            advposDto.setAdvposnum(count1);
            return advposDto;
        }).collect(Collectors.toList());
        advposDtoPage.setRecords(advposDtoslist);
        PageResult pageResult = PageResult.init((long) count, Long.valueOf(limit), Long.valueOf(page), advposDtoslist);
        log.info("数据是{}",pageResult.toString());
        return ResultData.ok("", pageResult);
    }

    /**
     * 广告位添加
     *
     * @param advpos
     * @return
     */
    @PostMapping("/add")
    public ResultData add(@RequestBody YddAdvpos advpos) {
        //advpos.setDefaultData("1");//设置初始值
        yddAdvposService.save(advpos);
        return ResultData.ok("", "成功");
    }

    /**
     * 广告位详情
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData row(int id) {
        YddAdvpos byId = yddAdvposService.getById(id);
        return ResultData.ok("", byId);
    }

    /**
     * 广告位修改
     *
     * @param advpos
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody YddAdvpos advpos) {
        yddAdvposService.updateById(advpos);
        return ResultData.ok("", "成功");
    }

    /**
     *广告位删除
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData delete(int id) {
        //如果有广告图绑定了这个广告位就不能删除
        LambdaQueryWrapper<YddAdvimg> lqw = new LambdaQueryWrapper<>();
        lqw.eq(YddAdvimg::getAdvimgpos, id);
        int count = yddAdvimgService.count(lqw);
        if (count>0){
            return ResultData.error("广告位有图片无法删除");
        }
        yddAdvposService.removeById(id);
        return ResultData.ok("", "成功");
    }

    /**
     *广告位列表-全部
     *
     * @return
     */
    @GetMapping("/all")
    public ResultData all() {
        List<YddAdvpos> list = yddAdvposService.list();
        return ResultData.ok("", list);
    }
}
